package com.sfh.story;

import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.sfh.story.entrty.Story;

/**
 * 获取内容
 * @author sWX229906
 *
 */
public class GetStoryContent
{
    
    public interface IStory
    {
        public void load(Story story);
    }
    
    private IStory mICallBack = null;
    
    private Story mStory;
    
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            if (mICallBack != null)
            {
                mICallBack.load(mStory);
            }
        }
    };
    
    public void start(Story story, IStory callBack)
    {
        this.mICallBack = callBack;
        this.mStory = story;
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                parseHtml();
            }
        }).start();
    }
    
    private void parseHtml()
    {
        try
        {
            URL mUrl = new URL("http://www.gsdaquan.com/erjiye/" + mStory.address);
            
            Document mDocument = Jsoup.parse(mUrl, 50000);
            Elements mElements = mDocument.select("div.left_neirong");
            
            String content = "";
            for (Element element : mElements)
            {
                String text = element.text();
                if (mStory != null)
                {
                    content = text;
                    System.out.println("Story content =" + content);
                }
            }
            
            int length = content.length();
            
            
            int z = length / 6;
            int y = length % 6;
            
            for (int i = 0; i < 6; i++)
            {
                String str = content.substring(i * z, (i + 1) * z);
                if (i == 5)
                {
                    if (y != 0)
                    {
                        str = content.substring(i * z);
                    }
                }
                
                System.out.println("Story  i =" + str);
                
                mStory.content.add(str);
            }
//            String imageURL =
//                String.format("http://image.baidu.com/i?tn=baiduimagejson&width=&height=&word=%s&rn=10&pn=2",
//                    URLEncoder.encode(mStory.name, "UTF-8"));
//            System.out.println("Story  imageURL=" + imageURL);
//            List<String> icon = requestByHttpGet(imageURL);
//            
//            System.out.println("Story  image 数量=" + icon.size());
//            mStory.imgs.addAll(icon);
            mHandler.sendEmptyMessage(0);
            
        }
        catch (Exception e)
        {
        }
        
       
    }
    
    // HttpGet方式请求
    public List<String> requestByHttpGet(String path)
    {
        List<String> icon = new ArrayList<String>();
        try
        {
            // 新建HttpGet对象
            HttpGet httpGet = new HttpGet(path);
            // 获取HttpClient对象
            HttpClient httpClient = new DefaultHttpClient();
            // 获取HttpResponse实例
            HttpResponse httpResp = httpClient.execute(httpGet);
            // 判断是够请求成功
            if (httpResp.getStatusLine().getStatusCode() == 200)
            {
                // 获取返回的数据
                String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
                if (result != null && result.length() > 0)
                {
                    JSONObject jsonObject = new JSONObject(result);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++)
                    {
                        String objURL = data.getJSONObject(i).getString("objURL");
                        icon.add(objURL);
                        System.out.println("Story image=" + objURL);
                    }
                }
            }
            else
            {
                System.out.println("Story HttpGet方式请求失败");
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
        }
     
       
        return icon;
    }
    
}
